import { native, sys, Node, TTFFont } from 'cc';
import GameLevel from '../model/GameLevel';

export default class Global {
	//android传过来的
	static deviceId: string = this.getDeviceId();
	// static deviceId: string = "1A2B"
	//接口路径
	static baseUrl = this.getBaseUrl();
    
	// static baseUrl = "http://192.168.3.5:10000/api/xpad/"
	// static baseUrl = "https://api.mollearning.com/api/xpad/"
	static URL_DAILY_ENTRANCE = this.baseUrl + '/learnDaily/show';
	static URL_GAME_LEVELS = this.baseUrl + '/game/levels';
	static URL_GAME_IS_CAN_PLAY = this.baseUrl + '/game/isCanPlay';

	static MESSAGE_SET_BGM_SWITCH = 'message_set_bgm_switch';
	static MESSAGE_SET_WIFi_STATUS = 'message_set_wifi_status';
	static MESSAGE_BIND_SUCCESS = 'message_bind_success';
	static MESSAGE_RESTORE_IP = 'message_restore_ip';
	static MESSAGE_GRADE_CHANGED = 'message_grade_changed';

	//当前闯关的关卡index,从0开始
	static currentLevelIndex: number | null = null;
	static gameLevels: GameLevel[] | null = null;

	static wifiStatus = this.getWifiStatus();

	static preloadGames: string[] = [
		'rapidCalculate',
		'apple',
		'run',
		'plane',
		'learnMaking10',
		'playMaking10',
		'divideTen',
		'divideTenPlay',
	];
	static games: string[] = ['apple', 'run', 'plane'];
	// static games: string[] = ['plane', 'plane','plane']
	static gameIndex: number = this.getRandomIntInRange(0, 2);

	static bgmSwitch = false;

	static gameOrderBy: number | null = null; // 十的分与合、凑十法游戏关卡
	static gameLevelId: number | null = null; // 十的分与合、凑十法游戏关卡
	static fastCalculateId: number | null = null;

	static systemFonts: TTFFont[] = [];
	static routeFrom: string = '';
	static touchId: number = null;

	// 进场景传参
	static routeParams: any = {};
	//把游乐场数据缓存到内存中，提升加载速度
	static parkData: string = '';
	// 星际旅行，弹窗游戏关卡
	static gameLevelList: any = {};

	static mainTouchId: number | null = null;
	static homeTouchId: number | null = null;
	static ticketTouchId: number | null = null;

	static getRandomIntInRange(min: number, max: number): number {
		min = Math.ceil(min); //向上取整
		max = Math.floor(max); //向下取整
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}

    static getBaseUrl(): string {
        // return "https://api.mollearning.com/api/xpad/"
        if (sys.platform == sys.Platform.ANDROID) {
            return native.reflection.callStaticMethod(
                "com/davinci/xpad/data/local/CocosLocal",
                "getBaseUrl",
                "()Ljava/lang/String;"
            );
        } else {
            return "http://192.168.3.5:10000/api/xpad/"
            // return "http://192.168.3.60:10000/api/xpad/"
        }
    }
    static netIsAvailable(): boolean {
        if (sys.platform == sys.Platform.ANDROID) {
            const isNetAvailable = native.reflection.callStaticMethod(
                "com/davinci/xpad/data/local/CocosLocal",
                "netIsAvailable",
                "()Z"
            );
            return isNetAvailable
        } else {
            return true
        }
    }

	static getDeviceId(): string {
		if (sys.platform == sys.Platform.ANDROID) {
			return (this.deviceId = native.reflection.callStaticMethod(
				'com/davinci/xpad/data/local/CocosLocal',
				'getDeviceId',
				'()Ljava/lang/String;'
			));
		} else {
			return '81e5f4862b304dd1';
		}
	}

	static getWifiStatus(): string {
		if (sys.platform == sys.Platform.ANDROID) {
			return native.reflection.callStaticMethod(
				'com/davinci/xpad/data/local/CocosLocal',
				'getWifiStatus',
				'()Ljava/lang/String;'
			);
		} else {
			return 'off';
		}
	}

	// 遍历场景中所有节点
	static removeAllTouchEndListeners(rootNode: Node) {
		rootNode.walk((node) => {
			// 移除所有 TOUCH_END 事件监听器
			node.off(Node.EventType.TOUCH_END);
		});
	}
}
